package com.zha;

import com.google.gson.Gson;
import org.apache.avro.data.Json;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"1","username":"wangcc","age":"18"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"2","username":"lizhe","age":"10"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"3","username":"shijiawei","age":"20"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"4","username":"jinnenghui","age":"30"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"5","username":"liuxiaoqi","age":"50"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"6","username":"yaoshuai","age":"13"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"7","username":"zhoucong","age":"40"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"8","username":"siguogong","age":"54"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"9","username":"xiaoyu","age":"487"}]}
//{"header":"asdfsadfsadfsafdsadfasfdsadfas","date":"2018-12-11 14:30","data":[{"id":"10","username":"dujinyu","age":"12"}]}
public class json {
    public static class mapjson extends Mapper<LongWritable, Text,Text, NullWritable>{

        static  Connection connection = JDBCutils.getConnection();

        /**
         * 1.获取文件
         * 2.解析每一行的json得到实体类hello
         * 3.得到id  age  username 后  建立  jdbc链接
         * 4.创建链接起，
         * 5.发送
         *
         * @param key
         * @param value
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            Gson gson = new Gson();

            Hello hello = gson.fromJson(value.toString(),Hello.class);

            List<Hello.DataEntity> data = hello.getData();

            //第一中 增强for循环
            for (Hello.DataEntity datum : data) {
                   String id = datum.getId();
                   String age = datum.getAge();
                   String usernaem = datum.getUsername();
                   String sql =  "insert into user value(?,?,? );";
                   insert(id, age, usernaem, sql);
            }
//            //第2中
//            for (int i = 0; i < data.size(); i++) {
//                Hello.DataEntity dataEntity = data.get(i);
//            }
//
//            //第3中 迭代遍历
//            Iterator<Hello.DataEntity> iterator = data.iterator();
//            while (iterator.hasNext()){
//                Hello.DataEntity dataEntity = iterator.next();
//            }
//
            return;
        }

        /**
         * 添加数据方法
         * @param id
         * @param age
         * @param usernaem
         * @return
         */
        public static void insert(String id,String age,String usernaem,String sql){
            try {
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,id);
                preparedStatement.setString(2,usernaem);
                preparedStatement.setString(3,age);
                int i = preparedStatement.executeUpdate();
                if (i == 1){
                    System.out.println("添加成功");
                }else {}
                System.out.println("添加失败");

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = new Job();

        job.setJarByClass(json.class);
        job.setMapperClass(mapjson.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        File file = new File("d:/file1/output");
        if (file.exists()) {
            FileUtil.fullyDelete(file);
        }
        FileInputFormat.addInputPath(job,new Path("d:/file1/input"));
        FileOutputFormat.setOutputPath(job,new Path("d:/file1/output"));

        job.setNumReduceTasks(1);
        System.exit(job.waitForCompletion(true)?0:1);
    }




}
